package com.smart.manager.finance.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.smart.common.entities.core.BaseSearch;
import com.smart.common.entities.core.Page;
import com.smart.manager.finance.entities.bodys.RCurrencyTariff;
import com.smart.manager.finance.entities.models.FinanceCurrencyTariff;
import com.smart.manager.finance.entities.models.query.QFinanceCurrencyTariff;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 支付中心-订单管理
 *
 * @author Administrator
 */

@Log4j2
@Service
@RequiredArgsConstructor
public class CurrencyTariffRepository {


    public Page<FinanceCurrencyTariff> pageList( BaseSearch search) {
       
        QFinanceCurrencyTariff query = new QFinanceCurrencyTariff();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows()).order().payAmount.asc();
        return new Page<FinanceCurrencyTariff>().load(query.findPagedList());
    }

    /**
     * 添加
     *
     * @param form 形式
     */
    public void add(RCurrencyTariff.EditBody form) {
        FinanceCurrencyTariff paymentWechat = BeanUtil.copyProperties(form, FinanceCurrencyTariff.class);
        paymentWechat.save();
    }

    /**
     * 更新
     *
     * @param id   id
     * @param form 形式
     */
    public void update(Long id, RCurrencyTariff.EditBody form) {
        FinanceCurrencyTariff record = new QFinanceCurrencyTariff().id.eq(id).findOne();
        Assert.notNull(record, "微信支付账户配置不存在");
        BeanUtil.copyProperties(form, record);
        record.update();
    }

    /**
     * 批量删除
     *
     * @param idList id列表
     * @return int
     */
    public int delByIds(List<Long> idList) {
        return DB.deleteAll(FinanceCurrencyTariff.class, idList);
    }


    /**
     * 获取所有支付配置列表
     *
     * @return {@link List}<{@link FinanceCurrencyTariff}>
     */
    public List<FinanceCurrencyTariff> list() {
        return new QFinanceCurrencyTariff().order().payAmount.asc().findList();
    }


    public FinanceCurrencyTariff getOne(Long tariffId) {
        FinanceCurrencyTariff currencyTariff = new QFinanceCurrencyTariff().id.eq(tariffId).setMaxRows(1).findOne();
        Assert.notNull(currencyTariff  , "不存在的充值价目表数据" ) ;
        return currencyTariff;
    }
}
